<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Client_Model extends CI_Model {
	
    private $response;
	private $table= 'clients';
	function __construct() {
		parent::__construct();
	}
    
    public function getResponse() {
        return $this->response;
    }
	
	function find ($api){
		list($key,$code) = explode('-',$api);
    	$query = $this->db->get_where($this->table, array('api' => $key,'code'=>$code),1);
    	$result = $query->result();
    	return !empty($result)?$result[0]:false;
	}
	
	function getRates($api){
		$client = $this->find($api);
		$this->db->select("r.*");
		$this->db->from('client_rates as r');
		$this->db->join('clients as c','c.id = r.client_id','left');
		$this->db->where('r.client_id', $client->id);
		$query = $this->db->get();
		return $query->result();
	}
	
	function getSettings($api){
		$client = $this->find($api);
		$this->db->select("*");
		$this->db->from('client_settings');
		$this->db->where('client_id', $client->id);
		$query = $this->db->get();
		return $query->result();
	}
	
	function getSettingsByClientId($client_id){
		$this->db->select("*");
		$this->db->from('client_settings');
		$this->db->where('client_id', $client_id);
		$query = $this->db->get();
		return $query->result();
	}
	
	function getClientHolidays($array = null){
		if($array != null){
			$this->db->select("h.*, ht.*");
			$this->db->from("client_holidays as ch");
			$this->db->join('holidays as h','h.id = ch.holiday_id','left');
			$this->db->join('holiday_type as ht','ht.id = h.holiday_type','left');
			$this->db->where('ch.client_id', $array['client_id']);
			if(isset($array['type'])){
				$this->db->where('ht.type_name', $array['type']);
			}
			if(isset($array['date_start']) && isset($array['date_end'])){
				$this->db->where('h.date_start >=', $array['date_start']);
				$this->db->where('h.date_end <=', $array['date_end']);
			}
			$query = $this->db->get();
			return $query->result();
		} 
		
	}
    
    
    public function insertOrUpdate($data)
    {
        // get id to update if given
        $client_id = isset($data['id']) ? $data['id'] : '';
        
        // prepare insert data
        $db_data = array(
            'name'      => $data['name'],
            'api'       => isset($data['api']) ? $data['api'] : '',
            'code'      => $data['code'],
            'key'       => isset($data['key']) ? $data['key'] : '',
            'status'    => isset($data['status']) ? $data['status'] : 'active',
            'parent_id' => isset($data['parent_id']) && is_numeric($data['parent_id']) ? $data['parent_id'] : 0
        );
        
        if ( empty($client_id)) {
            // first check if a client name already exists under this client
            $query = $this->db->get_where($this->table, array('name' => $data['name']));
            if ($query->num_rows() > 0)
            {
                $this->response = array('status' => 'failed', 'message' => 'Client name already exists.');
                return false;
            }
            
            $return = $this->db->insert($this->table, $db_data);
        } else {
            $this->db->where('id', $client_id);
            $this->db->where('parent_id', $data['parent_id']);
            $return = $this->db->update($this->table, $db_data);
        }
        
        return $return;
    }
	
    
    public function getClientList($parent_client_id = false)
    {
        if (empty($parent_client_id) && $parent_client_id !== false) {
            $this->db->where('parent_id', '0');
        }
        
        if ( ! empty($parent_client_id)) {
            $this->db->where('parent_id', $parent_client_id);
        }
        
        $this->db->select('c.*, COUNT(u.id) total_users');
        $this->db->from($this->table.' c');
        $this->db->join('users u', 'c.id = u.client_id AND u.status = 1', 'left');
        $this->db->group_by('c.id');
        $this->db->order_by('name ASC'); // ordering
        
        return $this->db->get()->result_array();
    }
    
    
    public function getClientDetails($client_id)
    {
        return $this->db->get_where($this->table, array('id' => $client_id))->row_array();
    }
	
}